<template>
	<om-addpage style="padding-top: 35px;" :menuName="menuName" @formSubmit="formSubmit" :busCode="busCode">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		data() {
			return buildFormData({
				DICT_SOCIAL_SECURITY: '',
				DICT_ADJUST_TYPE6_1: '',
				DICT_ADJUST_TYPE6_2: '',
				DICT_ADJUST_TYPE6_3: '',
				DICT_ADJUST_TYPE6_4: '',
				OUT_CATEID: [],
				ALL_CATEID: [],
				adjustCode: "",
				cateNames: "",
				dicname: "",
				cateList: [],
				returnCateList: [],
				adjustName: '',
				firstAdjustName: "",
				returnList:'',
				returnNames:'',
				userCacheKey:"hr-wf-security-user-cache"
			})
		},
		provide() {
			return {
				formPage: this
			}
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods: {
			afterUser(id){
				let _that = this
				_that.om.request(
					"tsocialSecurityApply/afterUser",
					{
						userId:id
					}
				).then(res=>{
					if(res.value.user){
						let formdata = _that.$refs.autoform.formdata
						let d = res.value.user
						formdata.entryDate = d.entryDate
						formdata.sexName = d.sexName
						formdata.idCard = d.idCard
						formdata.salaryPosName = d.salaryPosName
						formdata.instName = d.instName
						formdata.posGrade = d.posGrade
					}
				})
			},
			formSubmit() {
				let _that = this
				if(_that.$refs.autoform.formdata.cateIds === ''){
					_that.om.toast('社保类别不能为空,请选择社保类别',5000)
					return
				}
				if(_that.$refs.autoform.formdata.adjustName === '退保' && !_that.edit){
					_that.$refs.autoform.formdata.cateIds = _that.returnList
					_that.$refs.autoform.formdata.cateName = _that.returnNames
				}
				if(!_that.checkedit){
					_that.om.formSave('tsocialSecurityApply/save', _that.$refs.autoform.formdata, _that);
				}else{
					_that.om.formSave('commonForm/doCheckOrApproveUpdate', _that.$refs.autoform.formdata, _that);
				}
			},

			getCbCateList(code) {
				let _that = this
				let obj = {
					code: code,
				}
				_that.om.request(
					'tsysdict/getListByCode',
					obj,
					"post"
				).then(res => {
					let obj2 = {
						checked: true
					}
					if (res.length) {
						for (let item of res) {
							if (_that.edit) {
								if (_that.OUT_CATEID.indexOf(item.id) == -1) {
									Object.assign(item, obj2)
								}
							} else {
								if(_that.checkedit){
									_that.$set(item,'disabled',true)
								}
								Object.assign(item, obj2)
							}
						}
					}
					_that.$refs.autoform.changeEditor('itemInfo', 'cateIds', 'data', configSelectData(res, 'id', 'dicname', true))
				})
			},
			getUserCateList(list, dicname) {
				let _that = this
				let newOutCateId = _that.ALL_CATEID
				if (!_that.firstAdjustName) {
					_that.firstAdjustName = dicname
					newOutCateId = _that.OUT_CATEID
				} else if (_that.firstAdjustName === dicname) {
					newOutCateId = _that.OUT_CATEID
				}
				_that.$refs.autoform.changeEditor('itemInfo', 'cateIds', 'data', configSelectData([], '', '', true))
				let obj = {
					userId: _that.userId,
				}
				if (_that.edit) {
					let obj2 = {
						checked: true
					}
					let newStr = ''
					newStr = _that.cateNames
					let userCateNames = newStr.split(',')
					for (let item of list) {
						if(dicname === '退保' ){
							_that.$set(item,'disabled',true)
						}
						if (userCateNames.indexOf(item.dicname) != -1) {
							if (_that.edit) {
								if (newOutCateId.indexOf(item.id) == -1) {
									Object.assign(item, obj2)
								}
							} else {
								if (dicname === '调整参保项目') {
									Object.assign(item, obj2)
								}
							}
						}
					}
					_that.$refs.autoform.changeEditor('itemInfo', 'cateIds', 'data', configSelectData(list, 'id', 'dicname', true))
				} else {
					_that.om.request(
						'tsocialSecurityApply/getCateIds',
						obj,
						"post"
					).then(res => {
						_that.$refs.autoform.formdata.other= res.value.other
						let obj2 = {
							checked: true
						}
						let obj3 = {
							disabled:true
						}
						let newStr = ''
						newStr = res.value.cateIds
						_that.returnList = res.value.cateIds
						_that.returnNames = res.value.cateName
						let userCateIds = newStr.split(',')
						for (let item of list) {
							if(_that.checkedit || dicname === '退保'){
								_that.$set(item,'disabled',true)
							}
							if (userCateIds.indexOf(item.id + '') != -1) {
								if (_that.edit) {
									if (newOutCateId.indexOf(item.id) == -1) {
										if(dicname === '退保' || dicname === '调整金额'){
											Object.assign(item,obj2)
											Object.assign(item,obj3)
										}else{
											Object.assign(item, obj2)
										}
									}
								} else {
									if (dicname) {
										if(dicname === '退保' || dicname === '调整金额'){
											Object.assign(item, obj2)
											Object.assign(item, obj3)
										}else{
											Object.assign(item, obj2)
											
										}
									}
								}
							}else{
								if(dicname === '退保' || dicname === '调整金额'){
									Object.assign(item,obj3)
								}
							}
						}
						_that.$refs.autoform.changeEditor('itemInfo', 'cateIds', 'data', configSelectData(list, 'id', 'dicname', true))
					})
				}
			},
			onSelectAdjust(item) {
				let _that = this
				_that.$refs.autoform.clear('itemInfo', 'cateIds')
				let code = _that.DICT_SOCIAL_SECURITY
				_that.adjustName = item.dicname
				if (item.dicname === '参保') {
					_that.getCbCateList(code)
				} else {
					let _that = this
					let obj = {
						code: code,
					}
					_that.om.request(
						'tsysdict/getListByCode',
						obj,
						"post").then(res => {
						if (item.dicname === '调整参保项目') {
							_that.cateList = res
							_that.getUserCateList(_that.cateList, item.dicname)
						} else {
							_that.returnCateList = res
							_that.getUserCateList(_that.returnCateList, item.dicname)
						}
					})
				}
			},
			selectZposFunc(e) {
				let _that = this
				let obj = {
					zposId: e.id
				}
				_that.om.request(
					"wxSocialSecurity/afterZpos",
					obj,
					"get"
				).then(res => {
					if(res.value.user){
						_that.$refs.autoform.formdata.userName = _that.userName
						_that.$refs.autoform.formdata.userId = _that.userId
						_that.$refs.autoform.formdata.entryDate = res.value.user.entryDate
						_that.$refs.autoform.formdata.salaryPosName = res.value.user.salaryPosName
						_that.$refs.autoform.formdata.instName = res.value.user.instName
						_that.$refs.autoform.formdata.posGrade = res.value.user.posGrade
					}else{
						_that.$refs.autoform.changeEditor("userInfo","userId","type","linkbox")
						_that.$refs.autoform.changeEditor("userInfo","userId",{
							link:"../../public/pageSearch/pageSearch",
							"linkParam":{
								other:JSON.stringify({
									isContainLegal: false,
									isContainLeave: true,
									filterNullSalaryPos: false
								}),
								url:"temployee/getStaffCombogrid",
								initload:true,
								namefield:"username",
								extrafield:'userno',
								notefield:'salaryPosName',
								cachename:_that.userCacheKey,
								searchfield:'q'
							}
						})
					}
				})
				_that.getAdjustList()
			},
			clearZpos() {
				let _that = this
				_that.$refs.autoform.clear('topinfo', 'zposId')
			},
			getAdjustList(userId) {
				let _that = this
				let obj2 = {
					userId: userId?userId:_that.userId
				}
				_that.om.request(
					"wxSocialSecurity/getAdjustList",
					obj2,
					"get"
				).then(res => {
					_that.$refs.autoform.changeEditor('itemInfo', 'adjustCode', 'data', configSelectData(res, 'code', 'dicname'))
					if (_that.edit) {
						_that.$refs.autoform.initSelectIndex("itemInfo", "adjustCode")
					}
				})
			},

			initLoad(options) {
				let _that = this
				let ajaxArr = []
				var fileId = options.fileId
				ajaxArr.push(
				urlUtil.zposUrl(options.code),
				{
					url: "tsocialSecurityApply/afterUser",
					param: {
						userId: _that.userId
					},
					method: 'post'
				},{
					url:'thousingFund/getEffectMonthList',
					param:{
						ztime:moment().format('YYYY-MM-DD HH:mm')
					}
				})
				
				if (fileId) {
					ajaxArr.push({
						url: "tsocialSecurityApply/getInfo",
						param: {
							id: fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("itemInfo","effectMonth","data",configSelectData(res[2],'value','name'),mainData)
					if (res[1].value.tip && !_that.edit) {
						errorModal(_that, '提示信息', ignoreHtml(res[1].value.tip), function() {
							_that.clearZpos();
						})
					} else {
						if (res[0].length) {
							changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posName'), mainData)
						} else {
							_that.om.toast('您在OM系统中没有任职岗位，不能申请社保', 3000)
						}
					}
					if (fileId) {
						var code = _that.DICT_SOCIAL_SECURITY
						let obj = {
							code: code
						}
						var fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						var cateNames = ""
						for (let i in res[3].value.t) {
							if (fieldArr.includes(i)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, res[3].value.t[i])
								_that.$set(_that.formdata, i, res[3].value.t[i])
							}
						}
						_that.adjustCode = res[3].value.t.adjustCode
						_that.cateNames = res[3].value.t.cateName
						let newNameArr = _that.cateNames.split(',')
						_that.dicname = res[3].value.t.adjustName
						_that.om.request(
							'tsysdict/getListByCode',
							obj,
							"post"
						).then(result => {
							if (_that.$refs.autoform) {
								for (var item of result) {
									_that.ALL_CATEID.push(item.id)
									if (newNameArr.indexOf(item.dicname) === -1) {
										_that.OUT_CATEID.push(item.id)
									}
								}
							}
						})
						if(!_that.checkedit){
							_that.om.request(
								"wxSocialSecurity/afterZpos",
								{
									zposId:_that.$refs.autoform.formdata.zposId
								},
								"get"
							).then(res => {
								if(res.value.user){
									_that.$refs.autoform.formdata.userName = _that.userName
									_that.$refs.autoform.formdata.userId = _that.userId
									_that.$refs.autoform.formdata.entryDate = res.value.user.entryDate
									_that.$refs.autoform.formdata.salaryPosName = res.value.user.salaryPosName
									_that.$refs.autoform.formdata.instName = res.value.user.instName
									_that.$refs.autoform.formdata.posGrade = res.value.user.posGrade
								}else{
									changeEditor("userInfo","userId","type","linkbox",mainData)
									changeEditor("userInfo","userId",{
										link:"../../public/pageSearch/pageSearch",
										"linkParam":{
											other:JSON.stringify({
												isContainLegal: false,
												isContainLeave: true,
												filterNullSalaryPos: false
											}),
											url:"temployee/getStaffCombogrid",
											initload:true,
											namefield:"username",
											extrafield:'userno',
											notefield:'salaryPosName',
											cachename:_that.userCacheKey,
											searchfield:'q'
										}
									},null,mainData)
								}
							})
						}
						_that.$nextTick(function() {
							if(!_that.checkedit){
								_that.$refs.autoform.initSelectIndex("topinfo", "zposId")
							}
							_that.getAdjustList(_that.$refs.autoform.formdata.userId)
							_that.onSelectAdjust({
								code: _that.adjustCode,
								dicname: _that.dicname
							})	
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("itemInfo","effectMonth")
					})
				}, options)
			}
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userName = d.username
							formdata.userId = d.id
							_that.$refs.autoform.clear("itemInfo","adjustCode")
							_that.$refs.autoform.changeEditor("itemInfo","cateIds","data",configSelectData([],'id', 'dicname', true))
							_that.afterUser(d.id)
							_that.getAdjustList( d.id)
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(options) {
			let _that = this
			_that.om.request('wxSocialSecurity/getConst').then(res => {
				if (res.succeed) {
					_that.busCode = res.value.busCode || 'HRS1301';
					_that.DICT_SOCIAL_SECURITY = res.value.DICT_SOCIAL_SECURITY; //社保类别
					_that.DICT_ADJUST_TYPE6_1 = res.value.DICT_ADJUST_TYPE6_1; //参保
					_that.DICT_ADJUST_TYPE6_2 = res.value.DICT_ADJUST_TYPE6_2; //调整金额
					_that.DICT_ADJUST_TYPE6_3 = res.value.DICT_ADJUST_TYPE6_3; //退保
					_that.DICT_ADJUST_TYPE6_4 = res.value.DICT_ADJUST_TYPE6_4; //调整参保项目
					_that.initLoad(options);
				}
			});
		},
	}
</script>

<style>
</style>
